package com.tg.service.suzakuEventBus.processor;

import com.tg.service.suzakuEventBus.handler.MqEventHandler;
import com.tg.service.suzakuEventBus.handler.TaskEventHandler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * Created by linzc on 2018/9/20.
 */
//@Component
public class MessageBrokerHandlerBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {

        return bean;
    }

    @Autowired
    private MqEventHandler mqEventHandler;

    //对于每个容器执行了初始化的 bean，如果这个 bean 的某个方法注解了@Subscribe,则将该 bean 注册到事件总线
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {

        System.out.println("MessageBrokerHandlerBeanPostProcessor==" + beanName);

        if (beanName.toLowerCase().endsWith("taskeventhandler")) {
            TaskEventHandler taskEventHandler = (TaskEventHandler) bean;
//            taskEventHandler.setBeanName(beanName);
//            mqEventHandler.add(taskEventHandler);
        }

        return bean;
    }
}
